package pt.nmfa.peste.client;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import pt.nmfa.peste.client.bean.ActionParametersDO;
import pt.nmfa.peste.client.bean.DeviceActionsDO;
import pt.nmfa.peste.client.bean.DeviceDO;
import pt.nmfa.peste.client.services.devicemanagement.DeviceManagement;
import pt.nmfa.peste.client.services.devicemanagement.DeviceManagementAsync;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.data.RecordList;
import com.smartgwt.client.widgets.grid.ListGridRecord;

public class DeviceManagementClient {
	

	 
	public static String message="";
	static RecordList deviceList=null;
	static RecordList deviceActionList=null;
	
	public static RecordList getDevices()
    {
			
			deviceList=new RecordList();
			message="";
			getService().getDevices(new AsyncCallback<List<DeviceDO>>(){

          					public void onFailure(Throwable arg0) {
          						arg0.printStackTrace();
          						deviceList=null;
          						message=arg0.getMessage();
          					}
          
          					public void onSuccess(List<DeviceDO> list) {
          						
          						for(int i=0;i<list.size();i++)
          						{
          								ListGridRecord record=new ListGridRecord();
          								record.setAttribute("type", list.get(i).getType());
          								record.setAttribute("name", list.get(i).getName());
          								record.setAttribute("deviceId", list.get(i).getDeviceId());
          								deviceList.add(record);
        						}
          						
        					}
			});
		



			return deviceList;

    }
	
	
	public static RecordList getDeviceActionList(int deviceID)
    {
			deviceActionList=new RecordList(); 
			message="";
			getService().getDeviceActionsList(deviceID, new AsyncCallback<List<DeviceActionsDO>>(){

          					public void onFailure(Throwable arg0) {
          						arg0.printStackTrace();
          						deviceActionList=null;
          						message=arg0.getMessage();
          					}
          
          					public void onSuccess(List<DeviceActionsDO> list) {
          						
          						for(int i=0;i<list.size();i++)
          						{
          								ListGridRecord record=new ListGridRecord();
          								record.setAttribute("deviceActionsId", list.get(i).getDeviceActionsId());
          								record.setAttribute("name", list.get(i).getName());
          								record.setAttribute("actionType", list.get(i).getActionType());
          								deviceActionList.add(record);
        						}
          						
        					}
			});
		



			return deviceActionList;

    }   

	public static RecordList getDeviceActionParameterList(int deviceActionsID)
    {
			deviceActionList=new RecordList(); 
			message="";
			getService().getDeviceActionsParametersList(deviceActionsID, new AsyncCallback<List<ActionParametersDO>>(){

          					public void onFailure(Throwable arg0) {
          						arg0.printStackTrace();
          						deviceActionList=null;
          						message=arg0.getMessage();
          					}
          
          					public void onSuccess(List<ActionParametersDO> list) {
          						
          						for(int i=0;i<list.size();i++)
          						{
          								ListGridRecord record=new ListGridRecord();
          								record.setAttribute("name", list.get(i).getParameterName());
          								record.setAttribute("value", list.get(i).getParameterValue());
          								deviceActionList.add(record);
        						}
          						
        					}
			});
		



			return deviceActionList;

    }   
	 public static void updateDevice(DeviceDO deviceDO)
	 {

//		 deviceActionList=new RecordList();
		 message="";
		 getService().updateDevice(deviceDO, new AsyncCallback<Void>(){
				public void onSuccess(Void result) {
//					deviceActionList=getDeviceActionList(1);
				}

				public void onFailure(Throwable caught) {
					message=caught.getMessage();
					
				};
			});
//		 return deviceActionList;
	 }
	 
	 public static void importLircConfig(int deviceId,String file)
	 {
//		 deviceActionList=new RecordList();
		 message="";
		 getService().importLircConfig(deviceId,file, new AsyncCallback<Void>(){
				public void onSuccess(Void result) {
//					deviceActionList=getDeviceActionList(1);
					message="OK";
				}

				public void onFailure(Throwable caught) {
					message=caught.getMessage();
					
				};
			});
//		 return deviceActionList;
	 }
	 
	 public static void createDeviceAction(DeviceActionsDO deviceDO)
	 {
//		 deviceActionList=new RecordList();
		 message="";
		 getService().createDeviceActions(deviceDO, new AsyncCallback<Void>(){
				public void onSuccess(Void result) {
//					deviceActionList=getDeviceActionList(1);
				}

				public void onFailure(Throwable caught) {
					message=caught.getMessage();
					
				};
			});
//		 return deviceActionList;
	 }
	 
		public static void updateDeviceByRecord(Record deviceRecord) {
			DeviceDO deviceDO=new DeviceDO();
			deviceDO.setDeviceId(deviceRecord.getAttributeAsInt("deviceId"));
			deviceDO.setName(deviceRecord.getAttributeAsString("name"));
			deviceDO.setType(deviceRecord.getAttributeAsString("type"));
			updateDevice(deviceDO);
	}
		public static void updateDeviceByMap(Record oldDeviceRecord,Map deviceMap) {
			
			Iterator<String> it=deviceMap.keySet().iterator();
			while(it.hasNext())
			{
				String key=it.next();
				if(deviceMap.containsKey(key))
					oldDeviceRecord.setAttribute(key, deviceMap.get(key));
				
			}
					
			updateDeviceByRecord(oldDeviceRecord);
	}
		

	 private static DeviceManagementAsync getService() 
	   {       
	     String endpointURL = "http://127.0.0.1:8080/PesteAdmin/seam/resource/gwt2";
	     //String endpointURL = "http://microondas:8080/GenghisAdmin/seam/resource/gwt2";
		 //String endpointURL = GWT.getHostPageBaseURL() + "seam/resource/gwt2";
	      
	     DeviceManagementAsync svc = (DeviceManagementAsync) GWT.create(DeviceManagement.class);
	      ((ServiceDefTarget) svc).setServiceEntryPoint(endpointURL);
	      return svc;     
	   }
}
